package com.joshondesign.sdljava.gfx;

import com.joshondesign.sdljava.SDL;
import com.joshondesign.sdljava.SDL_Event;
import com.joshondesign.sdljava.SDL_EventType;

import java.util.ArrayList;
import java.util.List;

public class ListNode extends Control {
    private int width = 100;
    private int height = 100;
    private List<String> model = new ArrayList<String>();
    public int scrollY;
    int rowHeight = 30;

    public ListNode(final Scene scene) {
        scene.listen(Trigger.MouseDragged().setCallback(new Callback<SDL_Event>(){
            public int startX;
            public int startY;
            public int startScrollY;
            public int velocity = 0;
            public long startTick;

            public void call(SDL_Event sdl_event) {
                if(new Bounds(x,y,width,height).inside(sdl_event.getButton())) {
                    if(sdl_event.getType() == SDL_EventType.SDL_MOUSEBUTTONDOWN) {
                        startScrollY = scrollY;
                        startX = sdl_event.getButton().getX();
                        startY = sdl_event.getButton().getY();
                        startTick = SDL.SDL_GetTicks();
                    }
                    if(sdl_event.getType() == SDL_EventType.SDL_MOUSEMOTION) {
                        scrollY = sdl_event.getMotion().getY()-startY+startScrollY;
                    }
                    if(sdl_event.getType() == SDL_EventType.SDL_MOUSEBUTTONUP) {
                        scrollY = sdl_event.getMotion().getY()-startY+startScrollY;
                        long endTick = SDL.SDL_GetTicks();
                        long tdiff = endTick - startTick;
                        final double velocity = ((double)scrollY)/tdiff;
                        scene.animate(new Animation() {
                            double v = velocity*3;
                            @Override
                            public void update(long currentTime) throws Exception {
                                if(scrollY > 0 && v > 0) {
                                    v = -v;
                                }
                                if( scrollY < height-rowHeight*model.size() && v < 0) {
                                    v = -v;
                                }
                                scrollY+=(v*10);
                                if(v > 0.1 || v < -0.1) {
                                    v /= 1.1;
                                } else {
                                    stop();
                                }
                            }
                        });

                    }
                }
            }
        }));
    }

    public ListNode setWidth(int width) {
        this.width = width;
        return this;
    }

    public ListNode setHeight(int height) {
        this.height = height;
        return this;
    }

    public ListNode setModel(List<String> model) {
        this.model = model;
        return this;
    }

    @Override
    public void draw(GFX gfx) {
        gfx.setPaint(SColor.BLACK);
        gfx.translate(x,y);
        gfx.drawRect(0, 0, this.width, this.height);


        Bounds oldClip = gfx.getClipRect();
        gfx.setClipRect(new Bounds(x, y, width, height));
        for(int i=0; i<model.size(); i++) {
            gfx.drawRect(0,i*rowHeight+scrollY,this.width,rowHeight);
            gfx.drawString(font,""+i,20,20+i*rowHeight+scrollY);
        }
        gfx.setClipRect(oldClip);
    }

}
